package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import objects.Board;

/**
 * Servlet implementation class RoomContent
 */
@WebServlet(name = "RoomContent", urlPatterns = {"/RoomContent"})
public class RoomContentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoomContentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// Return the current room's content to the website.
		response.setContentType("application/json");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.flush();

		resultJson rj = new resultJson();
		rj.content = Board.getInstance().getCurrentRoomData(Integer.valueOf(request.getParameter("hoveredRoom")));
		rj.room = Board.getInstance().getRoomName(Integer.valueOf(request.getParameter("hoveredRoom")));
		
		out.print(new Gson().toJson(rj));
	}

	@SuppressWarnings("unused")
	private class resultJson
	{
		String room;
		String content;
	}
}

